﻿@using Think9.Models;
@{ ViewBag.Title = "Index";
    Layout = "~/Areas/Shared/_LayuiList.cshtml"; }

<input style="display:none" id="ReportId" value="@Model.ReportId">
<input style="display:none" id="FromTbId" value="@Model.TbId">
<table class="layui-table" id="tableOrder" lay-filter="tableFilter"> </table>
<script src="~/lib/layui/layui.js" charset="utf-8"></script>
<script>
    layui.use(["table", "form", "exLayer", "exUtils"], function () {
        let table = layui.table;
        let form = layui.form;
        let exLayer = layui.exLayer;
        let exUtils = layui.exUtils;

        let $ = layui.$;

        form.render(); //

        let OrderTable = table.render({
            elem: "#tableOrder",
            url: "/SysStats/ReportBasic/GetReportIndexOrder?rpId=" + $("#ReportId").val() + "&tbid=" + $("#FromTbId").val(),
            page: false,
            defaultToolbar: [{ title: '刷新', layEvent: '_refreshOrder', icon: 'layui-bg-gray layui-icon-refresh' }],
            toolbar: "<div class='layui-btn-container'><button type='button' class='layui-btn layui-btn-danger layui-btn-xs' lay-event='_delOrder'><i class='fa fa-trash-o'></i>删除</button><button type='button' class='layui-btn layui-btn-normal layui-btn-xs' lay-event='_addOrder'><i class='fa fa-plus'></i>新增</button><button type='button' class='layui-btn layui-btn-warm layui-btn-xs' lay-event='_editOrder'><i class='fa fa-pencil'></i>编辑</button><span class='layui-word-aux'><i class='fa fa-angle-double-left'></i>编辑排序</span></div>",
            cols: [[
                { type: "checkbox", fixed: 'left' },
                { templet: '<span>3<span>', hide: true },
                { field: "id", templet: "#id", hide: true },
                { field: "OrderNo", title: "排序", width: 100, templet: "#OrderNo" },
                { field: "IndexId", title: "字段", width: 200 },
                { field: "OrderType", title: "方式", width: 100, templet: "#OrderType" }
            ]],
            done: function (res, curr, count) {
                console.log(res, curr, count);
            }
        });

        table.on("toolbar(tableFilter)", function (obj) {
            switch (obj.event) {
                case "_addOrder":
                    addIndexOrder();
                    break;

                case "_editOrder":
                    editIndexOrder();
                    break;

                case "_delOrder":
                    delIndexOrder();
                    break;

                case "_refreshOrder":
                    refreshIndexOrder();
                    break;
            }
        });

        function addIndexOrder() {
            if ($("#FromTbId").val() == '') {
                layer.msg('请选择录入表');
                return false;
            }
            exLayer.openMiddle("添加排序字段", "/SysStats/ReportBasic/AddReportIndexOrder?rpId=" + $("#ReportId").val() + "&tbid=" + $("#FromTbId").val(), '500px', '450px', layui.device().mobile);
        }

        function editIndexOrder() {
            var _gridList = foreachGridTable('3');
            exUtils.ajax('/SysStats/ReportBasic/EditReportIndexOrder', 'post', { "gridlist": _gridList }, true).done(function (response) {
                OrderTable.reload({
                    url: "/SysStats/ReportBasic/GetReportIndexOrder?rpId=" + $("#ReportId").val() + "&tbid=" + $("#FromTbId").val()
                });
            }).fail(function (error) {
                console.log(error);
            });
        }

        function delIndexOrder() {
            var idsStr = '';
            var checkStatus = table.checkStatus('tableOrder');
            var rows = checkStatus.data.length;
            if (rows > 0) {
                for (var i = 0; i < checkStatus.data.length; i++) {
                    if (parseInt(checkStatus.data[i].Id) > 0) {
                        idsStr += checkStatus.data[i].Id + ',';
                    }
                }
                exLayer.confirm('确定要删除吗？', function (index) {
                    layer.close(index);
                    if (idsStr) {
                        exUtils.ajax('/SysStats/ReportBasic/BatchDelReportIndexOrder', 'post', { idsStr: idsStr }, true).done(function (response) {
                            OrderTable.reload({
                                url: "/SysStats/ReportBasic/GetReportIndexOrder?rpId=" + $("#ReportId").val() + "&tbid=" + $("#FromTbId").val()
                            });
                        }).fail(function (error) {
                            console.log(error);
                        });
                    }
                });
            }
            else {
                layer.msg('未选择有效数据');
                return false;
            }
        }

        function refreshIndexOrder() {
            OrderTable.reload({
                url: "/SysStats/ReportBasic/GetReportIndexOrder?rpId=" + $("#ReportId").val() + "&tbid=" + $("#FromTbId").val()
            });
        }

        function foreachGridTable(flag) {
            var _list = [];
            var trList = $(".layui-table").find("tr");  //获取table下的所有tr
            for (var i = 0; i < trList.length; i++) {  //遍历所有的tr
                var tdArr = trList.eq(i).find("td"); //获取该tr下的所有td

                var _flag = tdArr.eq(1).text();
                var _id = tdArr.eq(2).find('input').val();
                var _no = tdArr.eq(3).find('input').val();
                if (_no != '' && _flag == flag) {
                    var _row = {};
                    _row.id = _id;
                    _row.OrderNo = _no;
                    _list.push(_row);
                }
            }
            return _list;
        }

        function foreachGridTable2(flag) {
            var _list = [];
            var trList = $(".layui-table").find("tr");  //获取table下的所有tr
            for (var i = 0; i < trList.length; i++) {  //遍历所有的tr
                var tdArr = trList.eq(i).find("td"); //获取该tr下的所有td

                var _flag = tdArr.eq(1).text();
                var _id = tdArr.eq(2).find('input').val();
                var _no = tdArr.eq(3).find('input').val();
                var _width = tdArr.eq(7).find('input').val();
                if (_no != '' && _flag == flag) {
                    var _row = {};
                    _row.id = _id;
                    _row.OrderNo = _no;
                    _row.ColWidth = _width;
                    _list.push(_row);
                }
            }
            return _list;
        }

    });
</script>
<script type='text/html' id='id'>
    <input type="text" name="id" value="{{d.Id}}" class="layui-input">
</script>
<!-- 行工具栏模板 -->
<script type="text/html" id="DataType">
    {{# if(d.DataType == '1000'){ }}
    <span>datetime</span> {{# } }}

    {{# if(d.DataType == '2000'){ }}
    <span>string</span> {{# } }}

    {{# if(d.DataType == '2010'){ }}
    <span>string</span> {{# } }}

    {{# if(d.DataType == '2030'){ }}
    <span>string</span> {{# } }}

    {{# if(d.DataType == '2100'){ }}
    <span>string</span> {{# } }}

    {{# if(d.DataType == '2200'){ }}
    <span>string</span> {{# } }}

    {{# if(d.DataType == '2500'){ }}
    <span>string</span> {{# } }}

    {{# if(d.DataType == '3000'){ }}
    <span>int</span> {{# } }}

    {{# if(d.DataType == '3002'){ }}
    <span>decimal</span> {{# } }}

    {{# if(d.DataType == '3004'){ }}
    <span>decimal</span> {{# } }}

    {{# if(d.DataType == '3006'){ }}
    <span>decimal</span> {{# } }}

    {{# if(d.DataType == '3102'){ }}
    <span>decimal</span> {{# } }}

    {{# if(d.DataType == '5200'){ }}
    <span>img</span> {{# } }}
</script>
<script type="text/html" id="OrderType">
    {{# if(d.OrderType == '2'){ }}
    <span>倒序</span> {{# } }}
</script>
<script type="text/html" id="OrderNo">
    <input type='text' name='OrderNo' value='{{d.OrderNo}}' autocomplete='off' class='layui-input' maxlength='3' style='height:30px;' lay-verify='required|integer'>
</script>
<script type="text/html" id="ColWidth">
    <input type='text' name='ColWidth' value='{{d.ColWidth}}' autocomplete='off' class='layui-input' maxlength='3' style='height:30px;' lay-verify='required|integer'>
</script>